package Messaging;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import javax.microedition.lcdui.Display;
import main.MainCanvas;

//Class responsible to send the sms containing the rates of a specific currency
//(The user has to confirm that action first)
public class SMSSender
{
    private String phoneNumber;
    private String messageBody;
    private String contactName;
    
    private Display display;
    private Displayable previousDisplayable;
    private Displayable parentDisplayable;
    
    
    public SMSSender(String num, String msg, String name, Display display, Displayable displayable1, Displayable displayable2)
    {
        phoneNumber=num;
        messageBody=msg;
        contactName=name;
        
        this.display=display;
        previousDisplayable=displayable1;
        parentDisplayable=displayable2;
    }
    
    public void sendSMS()
    {
        Image alertImage=null;
        try
        {
            alertImage=Image.createImage("/images/"+MainCanvas.ciCanvas.currentCurrency.getName()+".png");
        }
        catch (Exception ex){}
        Alert smsConfirmation=new Alert("SMS confirmation", "Inform "+contactName+" for the "+MainCanvas.ciCanvas.currentCurrency.getName()+" rates?\n"+phoneNumber, alertImage, AlertType.CONFIRMATION);
        final Command noSMSCommand=new Command("No", Command.CANCEL, 0);
        smsConfirmation.addCommand(noSMSCommand);
        final Command yesSMSCommand=new Command("Yes", Command.OK, 1);
        smsConfirmation.addCommand(yesSMSCommand);
        smsConfirmation.setCommandListener(new CommandListener()
        {
            public void commandAction(Command c1, Displayable d1)
            {
                if (c1==noSMSCommand)
                {
                    display.setCurrent(previousDisplayable);
                }
                else if (c1==yesSMSCommand)
                {
                    SMSThread smsThread = new SMSThread();
                    smsThread.run();
                    display.setCurrent(parentDisplayable);
                }
            }
        });
        display.setCurrent(smsConfirmation);
    }
    
    
    private class SMSThread implements Runnable
    {
        public SMSThread()
        {
            
        }
        
        public void run()
        {
            String destination = "sms://"+phoneNumber;

            MessageConnection smsConnection = null;
            try
            {
                smsConnection = (MessageConnection) Connector.open(destination);
                TextMessage sms = (TextMessage)smsConnection.newMessage(MessageConnection.TEXT_MESSAGE);
                sms.setAddress(destination);
                sms.setPayloadText(messageBody);
                smsConnection.send(sms);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            if (smsConnection != null)
            {
                try
                {
                    smsConnection.close();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

}